﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BulletinModel;
using BulletinMvc.Lib;
using BulletinMvc.Models;
using Webdiyer.WebControls.Mvc;
namespace BulletinMvc.Controllers
{
    public class BulletinController : Controller
    {
        //
        // GET: /Bulletin/
        private BulletinDBEntities db;
        private IBulletinBL ibl;
        public BulletinController()
        {
            db = new BulletinDBEntities();
            ibl=  new BulletinBL(this.ModelState);//modelstate如何使用。有何用处
        }

        public ActionResult Index(int pagesize =10, int id = 1)
        {
            //BulletinViewModel model = new BulletinViewModel();
            //model.BulletinList = ibl.GetBulletinList();
            PagedList<Bulletin> bulletinlist;
            if(User.Identity.Name!="")
                  bulletinlist=ibl.GetBulletinList().ToPagedList(id,pagesize);
            else bulletinlist = ibl.GetAnonymousBulletinList().ToPagedList(id, pagesize);
           ViewData["YeMa"] = id;
           return View(bulletinlist);
        }

        //
        // GET: /Bulletin/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Bulletin/Create
       [Authorize]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Bulletin/Create

        [HttpPost]
       public ActionResult Create(BulletinCreateModel createmodel)
        {
            if (ModelState.IsValid)
            {
                if (createmodel.Duedatetime < createmodel.startdatetime)
                {
                    ModelState.AddModelError("", "结束时间必须大于开始时间!");
                    return View();
                }

                try
                {

                    ibl.saveBulletin(createmodel);
                    return RedirectToAction("Index");
                }
                catch(Exception ex)
                {
                    ModelState.AddModelError("", "添加出错！"+ex.Message);
                    return View();
                }


            }
            else {              
                return View();
            }

        }
        
        //
        // GET: /Bulletin/Edit/5
               [Authorize]
        public ActionResult Edit(int BulletinID, int BulletinYaMa, int YeMa)
        {
            ViewData["BulletinYaMa"] = BulletinYaMa;
            ViewData["BulletinID"] = BulletinID;
            ViewData["YeMa"] = YeMa;
            var Editbulletin = from a in db.Bulletins
                               where a.ID == BulletinID
                               select new BulletinCreateModel 
                               {
                                   
                                    name =a.Name,
                                     startdatetime=a.StartDate,
                                      Duedatetime=a.DueDate,
                                       StarFeedbackEnabled= a.StarFeedbackEnabled,
                                        TextFeedbackEnabled=a.TextFeedbackEnabled,
                                         visible=a.Visible,
                                          Description=a.Description                                                          
                               };
            return View(Editbulletin.SingleOrDefault());
        }

        //
        // POST: /Bulletin/Edit/5

        [HttpPost, ActionName("Edit")]
        public ActionResult EditComfirm(int BulletinID, int BulletinYaMa,int YeMa, BulletinCreateModel bulletin)
        {
            ViewData["BulletinYaMa"] = BulletinYaMa;
            ViewData["BulletinID"] = BulletinID;
            ViewData["YeMa"] = YeMa;
            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid) 
                {
                    if (bulletin.startdatetime >= bulletin.Duedatetime)
                    {
                        ModelState.AddModelError("", "开始时间应该小于结束时间!");
                        return View();
                    }
                    else
                    {

                        ibl.SaveEditBulletin(BulletinID, bulletin);
                        //return RedirectToAction("Index", new { id = BulletinYaMa });
                        //return RedirectToAction("Index", new { id = BulletinYaMa });
                        return RedirectToAction("Details","BulletinItem", new { BulletinID = BulletinID, BulletinYaMa = BulletinYaMa, id = YeMa });
                    }               
                
                
                }

 
               
            }
            catch
            {
                return View();
            }
            return View();
        }

        //
        // GET: /Bulletin/Delete/5
               [Authorize]
        public ActionResult Delete(int id, int BulletinYaMa)
        {
            ViewData["BulletinYaMa"] = BulletinYaMa;


            return View(ibl.DeleteDetail(id));
        }

        //
        // POST: /Bulletin/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteComfirm(int id, int BulletinYaMa)
        {
            try
            {
                // TODO: Add delete logic here
                if (ibl.DeleteBulletin(id))
                {
                    if (ibl.GetBulletinCount() / 1 < BulletinYaMa)
                        return RedirectToAction("Index", new { id = ibl.GetBulletinCount() / 1 });
                    else
                        return RedirectToAction("Index", new { id = BulletinYaMa });

                }
                else
                {
                    ViewData["BulletinYaMa"] = BulletinYaMa;
                    return View(ibl.DeleteDetail(id));

                 //   return View();
                }


            }
            catch
            {
                return View();
            }
        }
    }
}
